﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Dynamic;
using Microsoft.CSharp;
using System.Runtime.InteropServices.Automation;
using System.Windows.Data;
using Lite.ExcelLibrary.SpreadSheet;
using System.IO;

namespace KMT_Pro.Utils
{
    public class ExcelHelper
    {
        public void ExportToExcel(DataGrid sourceGrid, PagedCollectionView ItemsSourceCollection)
        {
            var exDataGrid = new DataGrid();
            int i = 0;
            int j = 0;
            
            exDataGrid.ItemsSource = ItemsSourceCollection;
            SaveFileDialog sDialog = new SaveFileDialog();
            sDialog.Filter = "Excel Files(*.xls)|*.xls";
            if (sDialog.ShowDialog() == true)
            {
                // create an instance of excel workbook
                Workbook workbook = new Workbook();
                // create a worksheet object
                Worksheet worksheet = new Worksheet("Friends");
                // write data in worksheet cells
                //foreach (DataGridCell dg in sourceGrid.ItemsSource)
                //{
                //};
                //foreach (var col2 in ItemsSourceCollection)
                //{
                //    worksheet.Cells[1, j] = new Cell(col2);
                //    j++;
                //}
                foreach (var col in sourceGrid.Columns)
                {
                    worksheet.Cells[0, i] = new Cell(col.Header);
                    i++;
                }
                
                workbook.Worksheets.Add(worksheet);
                Stream sFile = sDialog.OpenFile();
                // save method needs a stream object to write an excel file. 
                workbook.Save(sFile);
            }
            i = 0;

        }

    }
}
